import { ActivityAwardModel } from '@/models/activityAward'
import ActivityAwardTable from '@/pages/ActivityAward/components/ActivityAwardTable'
import ActivityAwardUpdateModal from '@/pages/ActivityAward/components/ActivityAwardUpdateModal'
import { ActivityAwardVO, saveActivityAward, updateActivityAward } from '@/services/ActivityAwardService'
import { useSnapshot } from '@@/exports'
import { PageContainer } from '@ant-design/pro-components'
import { ActionType } from '@ant-design/pro-table'
import { message } from 'antd'
import { useRef } from 'react'

const ActivityAward = () => {
  // 表格引用
  const tableRef = useRef<ActionType>()
  let activityAwardState = useSnapshot(ActivityAwardModel.state)

  // 提交修改
  const onUpdateFinish = async (formData: any) => {
    let dto: ActivityAwardVO = JSON.parse(JSON.stringify(formData))
    // 字段处理

    let func = activityAwardState.activityAward ? updateActivityAward : saveActivityAward
    if (await func(dto)) {
      message.success('操作成功')
      // 刷新表格
      tableRef.current?.reload()
      return true
    }
    return false
  }

  return (
    <PageContainer title="活动奖品">
      {/*活动奖品表格 开始*/}
      <ActivityAwardTable tableRef={tableRef} />
      {/*活动奖品表格 结束*/}

      {/*修改模态框 开始*/}
      <ActivityAwardUpdateModal onFinish={onUpdateFinish} />
      {/*修改模态框 结束*/}
    </PageContainer>
  )
}

export default ActivityAward
